package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.junit.Assert;

import pojo.User;
import check.imp.CheckImp;
import dao.imp.UserDaoImp;

/**
 * Servlet implementation class RegisterServlet
 */
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String info;
		UserDaoImp udi = new UserDaoImp();
		CheckImp ci = new CheckImp();
		String uname=request.getParameter("uname");
		double sal=ci.checkSal(request.getParameter("sal"));
		Date birth=ci.checkBirth(request.getParameter("birth"));
		String sex=request.getParameter("sex");;
		if(udi.loadUser(uname)!=null){
			info = "注册失败！该用户名已经存在！";
			request.getSession().setAttribute("info", info);
			response.sendRedirect("error");
		}else if(sal==-1){
			info = "注册失败！该月薪输入格式错误！";
			request.getSession().setAttribute("info", info);
			response.sendRedirect("error");
		}else if (birth==null) {
			info = "注册失败！该生日输入格式错误！";
			request.getSession().setAttribute("info", info);
			response.sendRedirect("error");
		}else{
			User user = null;
			if("1".equals(sex)){
				user = new User(uname, sal, birth, false);
				int m =udi.addUser(user);
				System.out.println(m);
			}else{
				user = new User(uname, sal, birth, false);
				int m=udi.addUser(user);
				System.out.println(m);
			}
			info = "注册成功！！";
			request.getSession().setAttribute("info", info);
			request.getSession().setAttribute("user", user);
			response.sendRedirect("showinfo");
		}
		System.out.println(info);
		
	}

}
